#include "main.h"


int main()
{
    init();
    tryLogin();
    return 0;
}

void init()
{
    log_file.open(LOG_PATH, std::ios::out);
}

void tryLogin()
{
    _log = new Login();
    username = _log->login();
    if(username != "")
    {
        system("clear");
        if(username == "root") _rootuser = new Rootuser(username);
        else _normaluser = new Normaluser(username);

        log_file << getNowTime() <<  (username + " login") << std::endl;
        Process();
    }
    else std::cout << "Login failed!" << std::endl;
    delete _log;
    _log = nullptr;
}

void rootLogin_info()
{
    switch(_rootuser->drawLoginStart())
    {
        case 1:
        {
            int ret = addAccount();
            if(ret == 1)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mAdd Successfully!" << std::endl;
                log_file << getNowTime() << username <<  ":  successfully add a new login information" << std::endl;
                sleep(2);
                rootLogin_info();
            }
            else if(ret == 0)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mInvalid User Name Or User ID" << std::endl;
                log_file << getNowTime() << username <<  ":  fail to add new login information" << std::endl;
                sleep(2);
                rootLogin_info();
            }
            else rootLogin_info();
        };break;
        case 2:
        {
            std::string ret = deleteAccount();
            if(ret != "" && ret != "Administrator" && ret != "q")
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mDelete Successfully!" << std::endl;
                log_file << getNowTime() << username <<  ":  successfully delete a login information" << std::endl;
                sleep(2);
                rootLogin_info();
            }
            else if(ret == "")
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mInvalid User ID" << std::endl;
                log_file << getNowTime() << username <<  ":  fail to delete login information" << std::endl;
                sleep(2);
                rootLogin_info();
            }
            else if(ret == "q") rootLogin_info();
            else
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mCan Not Delete Root User" << std::endl;
                log_file << getNowTime() << username <<  ":  fail to delete login information(root user)" << std::endl;
                sleep(2);
                rootLogin_info();
            }
        };break;
        case 3:
        {
            Process();
            return;
        };break;
        case 4:
        {
            system("clear");
            log_file << getNowTime() << username <<  ":  exit system" << std::endl;
            log_file.close();
            return;
        }    
    }
}

void rootStudent_info()
{
    switch(_rootuser->drawStudentStart())
    {
        case 1:
        {
            int ret = addStudent();
            if(ret == 1)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mAdd Successfully!" << std::endl;
                log_file << getNowTime() << username <<  ":  successfully add a new student" << std::endl;
                sleep(2);
                rootStudent_info();
            }
            else if(ret == 0)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mInvalid User Name Or Usr ID" << std::endl;
                log_file << getNowTime() << username <<  ":  fail to add a new student" << std::endl;
                sleep(2);
                rootStudent_info();
            }
            else rootStudent_info();
        };break;
        case 2:
        {
            int ret = deleteStudent();
            if(ret == 1)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mDelete Successfully!" << std::endl;
                log_file << getNowTime() << username <<  ":  successfully delete a student" << std::endl;
                sleep(2);
                rootStudent_info();
            }
            else if(ret == 0)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mInvalid User ID" << std::endl;
                log_file << getNowTime() << username <<  ":  fail to delete a student" << std::endl;
                sleep(2);
                rootStudent_info();
            }
            else rootStudent_info();
        };break;
        case 3:
        {
            int ret = modifyStudent();
            if(ret == 1)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mModify Successfully!" << std::endl;
                log_file << getNowTime() << username <<  ":  successfully modify a student information" << std::endl;
                sleep(2);
                rootStudent_info();
            }
            else if(ret == 0)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mFail To Modify" << std::endl;
                log_file << getNowTime() << username <<  ":  fail to modify student information" << std::endl;
                sleep(2);
                rootStudent_info();
            }
            else rootStudent_info();
        };break;

        case 4:
        {
           if(!showSpecificStudent())
           {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mNo Such Person" << std::endl;
                log_file << getNowTime() << username <<  ":  fail to show specific student information" << std::endl;
                sleep(2);
                rootStudent_info();
           }
           else
           {
                log_file << getNowTime() << username <<  ":  successfully show specific student information" << std::endl;
                rootStudent_info();
           }
        };break;
        case 5:
        {
            Process();
            return;
        };break;
        case 6:
        {
            system("clear");
            log_file << getNowTime() << username <<  ":  exit system" << std::endl;
            log_file.close();
            return;

        };break;      
    }
}

void rootSubject_info()
{
    switch(_rootuser->drawSubjectStart())
    {
        case 1:
        {
            int ret = addSubject();

            if(ret == 1)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mAdd Successfully!" << std::endl;
                log_file << getNowTime() << username <<  ":  successfully add a new subject" << std::endl;
                sleep(2);
                rootSubject_info();
            }
            else if(ret == 0)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mInvalid User Name Or Usr ID" << std::endl;
                log_file << getNowTime() << username <<  ":  fail to add a new subject" << std::endl;
                sleep(2);
                rootSubject_info();
            }
            else rootSubject_info();
        };break;
        case 2:
        {
            int ret = deleteSubject();
            if(ret == 1)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mDelete Successfully!" << std::endl;
                log_file << getNowTime() << username <<  ":  successfully delete a subject" << std::endl;
                sleep(2);
                rootSubject_info();
            }
            else if(ret == 0)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mInvalid User ID" << std::endl;
                log_file << getNowTime() << username <<  ":  fail to delete a subject" << std::endl;
                sleep(2);
                rootSubject_info();
            }
            else rootSubject_info();
        };break;
        case 3:
        {
            int ret = modifySubject();
            if(ret == 1)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mModify Successfully!" << std::endl;
                log_file << getNowTime() << username <<  ":  successfully modify subject information" << std::endl;
                sleep(2);
                rootSubject_info();
            }
            else if(ret == 0)
            {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mFail To Modify" << std::endl;
                log_file << getNowTime() << username <<  ":  fail to modify subject information" << std::endl;
                sleep(2);
                rootSubject_info();
            }
            else rootSubject_info();
        };break;

        case 4:
        {
            int ret = showSpecificSubject();
           if(ret == 0)
           {
                system("clear");
                std::cout << "\n\n\t\t\033[01;31mNo Such Person" << std::endl;
                log_file << getNowTime() << username <<  ":  fail to show specific subject information" << std::endl;
                sleep(2);
                rootSubject_info();
           }
           else if(ret == 1)
           {
                log_file << getNowTime() << username <<  ":  successfully show specific a subject information" << std::endl;
                rootSubject_info();
           }
           else rootSubject_info();
        };break;
        case 5:
        {
            Process();
            return;
        };break;
        case 6:
        {
            system("clear");
            log_file << getNowTime() << username <<  ":  exit system" << std::endl;
            log_file.close();
            return;

        };break;      
    }
}

void normalChangePassword()
{
    int ret = _normaluser->drawChangePasswordStart();
    if(ret == 0)
    {
        system("clear");
        std::cout << "\n\n\t\t\033[01;31mWrong Password!" << std::endl;
        log_file << getNowTime() << username <<  ":  fail to change normal user's password(two passwords not match)" << std::endl;
        sleep(2);
        Process();
    }
    else if(ret == 1)
    {
        system("clear");
        std::cout << "\n\n\t\t\033[01;31mDifferent Confirm Password!" << std::endl;
        log_file << getNowTime() << username <<  ":  fail to change normal user's password(old password error)" << std::endl;
        sleep(2);
        Process();
    }
    else 
    {
        system("clear");
        std::cout << "\n\n\t\t\033[01;31mChange Successfully!" << std::endl;
        log_file << getNowTime() << username <<  ":  successfully change normal user's password" << std::endl;
        sleep(2);
        Process();
    }
}

void normalSelectSubjects()
{
    int ret = _normaluser->drawSelectSubjectStart();
    if(ret == -1)
    {
        system("clear");
        std::cout << "\n\n\t\t\033[01;31mNo Subjects Left!" << std::endl;
        log_file << getNowTime() << username <<  ":  fail to select subject(no subject left)" << std::endl;
        sleep(2);
        Process();
    }
    else if(ret == 0)
    {
        system("clear");
        std::cout << "\n\n\t\t\033[01;31mNo Such Subjects For Chose!" << std::endl;
        log_file << getNowTime() << username <<  ":  fail to select subject(invalid subject SN code)" << std::endl;
        sleep(2);
        normalSelectSubjects();
    }
    else if(ret == 1)
    {
        system("clear");
        std::cout << "\n\n\t\t\033[01;31mSelect Successfully!" << std::endl;
        log_file << getNowTime() << username <<  ":  successfully select a subject" << std::endl;
        sleep(2);
        Process();
    }
    else
    {
        Process();
    }
}

void normalDropSubjects()
{
    int ret = _normaluser->drawDropSubjectStart();
    if(ret == -1)
    {
        system("clear");
        std::cout << "\n\n\t\t\033[01;31mYou Haven't Select Any Subject!" << std::endl;
        log_file << getNowTime() << username <<  ":  fail to drop subject(haven't select any subject)" << std::endl;
        sleep(2);
        Process();
    }
    else if(ret == 0)
    {
        system("clear");
        std::cout << "\n\n\t\t\033[01;31mYou Haven't Select This Subject!" << std::endl;
        log_file << getNowTime() << username <<  ":  fail to drop subject(haven't select this subject)" << std::endl;
        sleep(2);
        normalSelectSubjects();
    }
    else if(ret == 1)
    {
        system("clear");
        std::cout << "\n\n\t\t\033[01;31mDrop Successfully!" << std::endl;
        log_file << getNowTime() << username <<  ":  successfully drop a subject" << std::endl;
        sleep(2);
        Process();
    }
    else
    {
        Process();
    }
}

void Process()
{
    int ret = menu.drawMenu((username == "root"));
    if(username == "root")
    {
        if(ret == 1)
        {
            rootLogin_info();
        }
        else if(ret == 2)
        {
            rootStudent_info();
        }
        else if(ret == 3)
        {
            rootSubject_info();
        }
        else if(ret == 4)
        {
            tryLogin();
        }
        else if(ret == 5)
        {
            system("clear");
            log_file << getNowTime() << username <<  ":  exit system" << std::endl;
            return;
        }
    }
    else
    {
        if(ret == 1)
        {
            normalChangePassword();
        }
        else if(ret == 2)
        {
            _normaluser->drawMySubectsStart();
            log_file << getNowTime() << username <<  ":  show my subjects" << std::endl;
            Process();
        }
        else if(ret == 3)
        {
            normalSelectSubjects();
        }
        else if(ret == 4)
        {
            normalDropSubjects();
        }
        else if(ret == 5)
        {
            tryLogin();
        }
        else if(ret == 6)
        {
            system("clear");
            log_file << getNowTime() << username <<  ":  exit system" << std::endl;
            return;
        }
    }
    
    
}